package com.nmu.app.Shrimp.veiw.javagame.render;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.nmu.app.Shrimp.veiw.javagame.util.KeyboardInput;

public class SimpleItemMove extends JFrame implements Runnable{
	private volatile boolean running;
	private Thread gameThread;
	private BufferStrategy bufferStrategy;
	private KeyboardInput keyboard;
	private int deltaX=0;
	private int deltaY=0;
	private List<Point> points;
	private int ListRound=0;
	
	protected void createAndShowGUI(){
		Canvas canvas=new Canvas();
		canvas.setSize(640,480);
		canvas.setBackground(Color.WHITE);
		canvas.setIgnoreRepaint(true);
		getContentPane().add(canvas);
		setTitle("simple movement");
		setIgnoreRepaint(true);
		pack();
		keyboard=new KeyboardInput();
		canvas.addKeyListener(keyboard);
		setVisible(true);
		canvas.createBufferStrategy(2);
		bufferStrategy=canvas.getBufferStrategy();
		canvas.requestFocus();
		gameThread=new Thread(this);
		gameThread.start();
	}
	
	
	
	
	
	
	
	@Override
	public void run() {
		running=true;
		while(running){
			gameLoop();
		}
		
	}
	public void gameLoop(){
		processInput();
		//updateObject;
		renderFrame();
		sleep(5);
	}
	
	private void renderFrame(){
		do{
			do{
				Graphics g=null;
				try{
					g=bufferStrategy.getDrawGraphics();
					//清除
					g.clearRect(0, 0, getWidth(), getHeight());
					//重绘
					render(g);
				}finally{
					if(null!=g)g.dispose();
				}
			}while(bufferStrategy.contentsRestored());
			bufferStrategy.show();
		}while(bufferStrategy.contentsLost());
	}
	private void render(Graphics g){
		g.setColor(Color.RED);
		
		if(ListRound>0){
			int currentX=deltaX;
			int currentY=points.get(points.size()-ListRound).y;
			int nextY=currentY;
			if(ListRound>1)
			nextY=points.get(points.size()-ListRound+1).y;
			
			//knock
			if(currentX>270&&currentX<350&&(currentY==270||(currentY<270&&nextY>270))
					&&ListRound<=points.size()/2){
				g.drawRect(currentX, 270, 30, 30);
			}else{
				g.drawRect(currentX, currentY, 30, 30);
				ListRound--;				
			}
		}else{
			g.drawRect(deltaX, deltaY, 30, 30);	
		}
		//障碍物
			g.drawRect(300, 300, 50, 10);
	}

	private void processInput(){
		keyboard.poll();
		if(keyboard.keyDown(KeyEvent.VK_UP)){
			if(0==ListRound)
			deltaY=deltaY>1?deltaY-1:deltaY/2;
			System.out.println("Y-1:"+deltaY);
		}
		if(keyboard.keyDown(KeyEvent.VK_DOWN)){
			if(0==ListRound)
			deltaY+=1;
			System.out.println("Y+1:"+deltaY);
		}
		if(keyboard.keyDown(KeyEvent.VK_LEFT)){
			deltaX=deltaX>2?deltaX-2:deltaX/2;
			System.out.println("X-2:"+deltaX);
		}
		if(keyboard.keyDown(KeyEvent.VK_RIGHT)){
			deltaX+=2;
			System.out.println("X+2:"+deltaX);
		}
		if(keyboard.keyDownOnce(KeyEvent.VK_C)){
			deltaY=deltaX=0;
		}
		if(keyboard.keyDownOnce(KeyEvent.VK_SPACE)){
			if(0==ListRound){
				points=getJumpList(deltaY);
				ListRound=points.size();
				System.out.println("jump once");
			}else{
				System.out.println("can't double jump!");
			}
			
		}
		
	}
	private void sleep(long sleep){
		try{
			Thread.sleep(sleep);
		}catch(InterruptedException e){}
	}
	protected void onWindowClosing(){
		try{
			running=false;
			gameThread.join();
		}catch( InterruptedException e){
			e.printStackTrace();
		}
		System.exit(0);
	}
	
	public List<Point> getJumpList(int currentY){
		//y=-(x^2)/16+256
		List<Point> points=new ArrayList<Point>();
		for(int i=-64;i<=64;i++){
			Point p=new Point(i,currentY-((4096-i*i)/16));
			points.add(p);
		}
		return points;
	}
	
	
	
	public static void main(String[] args) {
		final SimpleItemMove app=new SimpleItemMove();
		app.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				app.onWindowClosing();
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				app.createAndShowGUI();
			}
		});
	
	}
	
	
	
}
